/**
 * 
 */
package dataPersistence.dto;

import hitExceptions.InvalidDataException;

import java.util.Date;

import coreModel.Barcode;
import coreModel.HIT;
import coreModel.Item;
import coreModel.ProductContainer;

/**
 * Inherits from DataTransferObject
 *
 */
public class ItemDTO implements DataTransferObject<Item>
{
	
	private String barcode;
	private String productBarcode;
	private Date entryDate;
	private Date exitDate;
	private String containerID;

	public ItemDTO(Item i) 
	{
		convertFromModelObject(i);
	}

	public ItemDTO() 
	{
		//do nothing
	}

	@Override
	public Item convertToModelObject()
	{
		Item i;
		
		try {
			 i = new Item(new Barcode(barcode), 
					HIT.getInstance().getProductManager()
							.getProductByBarcode(new Barcode(productBarcode)),
					HIT.getInstance().getStorageUnitManager().getContainerByID(containerID));
			 System.out.println(i.getBarcode());
			 i.setEntryDate(entryDate);
			 if (exitDate != null)
				 i.setExitTime(exitDate);
		} 
		catch (InvalidDataException e) {
			i = null;
			e.printStackTrace();
		}
		
		return i;
	}

	@Override
	public void convertFromModelObject(Item modelObject)
	{
		barcode = modelObject.getBarcode().getValue();
		productBarcode = modelObject.getProduct().getBarcode().getValue();
		entryDate = modelObject.getEntryDate();
		exitDate = modelObject.getExitTime();
		if (modelObject.getParentContainer() != null)
			containerID = modelObject.getParentContainer().getID();
		else
			containerID = null;
	}
	
	/////////////getters and setters/////////////

	public String getBarcode() {
		return barcode;
	}

	public void setBarcode(String barcode) {
		this.barcode = barcode;
	}

	public String getProductBarcode() {
		return productBarcode;
	}

	public void setProductBarcode(String productBarcode) {
		this.productBarcode = productBarcode;
	}

	public Date getEntryDate() {
		return entryDate;
	}

	public void setEntryDate(Date entryDate) {
		this.entryDate = entryDate;
	}

	public Date getExitDate() {
		return exitDate;
	}

	public void setExitDate(Date exitDate) {
		this.exitDate = exitDate;
	}

	public String getContainer() {
		return containerID;
	}

	public void setContainer(String containerID) {
		this.containerID = containerID;
	}

}
